package com.ljh.aiplatform.scoring;

import com.ljh.aiplatform.common.ErrorCode;
import com.ljh.aiplatform.exception.BusinessException;
import com.ljh.aiplatform.exception.ThrowUtils;
import com.ljh.aiplatform.model.entity.App;
import com.ljh.aiplatform.model.entity.UserAnswer;
import com.ljh.aiplatform.model.enums.AppTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScoringStrategyExecutor {


    @Resource
    private List<ScoringStrategy> strategies;

    public UserAnswer doScore(List<String> choices, App app) {
        final Integer appType = app.getAppType();
        final AppTypeEnum enumByValue = AppTypeEnum.getEnumByValue(appType);
        ThrowUtils.throwIf((enumByValue == null||appType==null), ErrorCode.PARAMS_ERROR,"appType参数错误");
        for (ScoringStrategy strategy : strategies) {
            if(strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
                ScoringStrategyConfig annotation = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if(annotation.appType()==app.getAppType()&&annotation.scoringStrategy()==app.getScoringStrategy())
                    return strategy.doScoring(choices,app);
            }
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
    }
}
